package com.hd.service.activiti.impl;

import com.hd.dao.BaseDao;
import com.hd.entity.activiti.ProcessInsCc;
import com.hd.entity.activiti.ProcessInsCcRead;
import com.hd.service.activiti.ProcessInsCcService;
import com.hd.service.activiti.extenum.ProccessInsCcStatus;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("processInsCcService")
public class ProcessInsCcServiceImpl implements ProcessInsCcService {
    @Autowired
    private BaseDao<ProcessInsCc, String> processInsCcDao;
    @Autowired
    private BaseDao<ProcessInsCcRead, String> processInsCcReadDao;

    @Override
    public void save(ProcessInsCc processinsCc) {
        processInsCcDao.save(processinsCc);
        if (StringUtils.isNotNull(processinsCc.getUserIdCcs())) {
            String[] userIdCcArr = processinsCc.getUserIdCcs().split(",");
            for (String userIdCc : userIdCcArr) {
                ProcessInsCcRead vo = new ProcessInsCcRead();
                vo.setObjId(processinsCc.getId());
                vo.setUserIdCc(userIdCc);
                processInsCcReadDao.save(vo);
            }
        }
    }

    @Override
    public void selectDataGrid(PageInfo info, ProcessInsCc processinsCc) {
        Map<String, Object> condition = info.getCondition();
        List<Object> params = new ArrayList<Object>();
        String hql = "from ProcessInsCc where 1=1 ";
        if (StringUtils.isNotNull(processinsCc.getTitle())) {
            hql += " and title like ?";
            params.add("%" + processinsCc.getTitle() + "%");
        }
        if (StringUtils.isNotNull(processinsCc.getProcessInstanceId())) {
            hql += " and processInstanceId=?";
            params.add(processinsCc.getProcessInstanceId());
        }
        if (StringUtils.isNotNull(processinsCc.getCreateTimeStart())) {
            hql += " and createTime>=?";
            params.add(processinsCc.getCreateTimeStart());
        }
        if (StringUtils.isNotNull(processinsCc.getCreateTimeEnd())) {
            hql += " and createTime<=?";
            params.add(processinsCc.getCreateTimeEnd());
        }
        String objIds=getObjIds(processinsCc.getUserIdCcs(),processinsCc.getReadStatus());
        if(StringUtils.isNotNull(objIds)) {
            hql += " and id in("+StringUtils.convertSingleStr(objIds)+")";
        }else{
            hql+=" and id=''";
        }
        hql+=" order by createTime desc";
        List<ProcessInsCc> rows = processInsCcDao.findPage(hql, info.getNowpage(), info.getPagesize(), params);
        if (rows.size()>0){
            convert(rows,processinsCc.getUserIdCcs());
        }

        String countHql = "select count(*) " + hql;
        Long count = processInsCcDao.getCountByList(countHql, params);
        int total = Integer.parseInt("" + count);
        info.setTotal(total);
        info.setRows(rows);
    }

    private String getObjIds(String userIdCc,Integer status){
        String objIds="";
        String hql="from ProcessInsCcRead where 1=1 and userIdCc=? ";
        if(status==ProccessInsCcStatus.READ.getStatus()){
           hql+=" and readTime is not null";
        }
        if(status==ProccessInsCcStatus.NOREAD.getStatus()){
            hql+=" and readTime is null";
        }
       List<ProcessInsCcRead>processInsCcReads= processInsCcReadDao.find(hql,userIdCc);
        if(processInsCcReads.size()>0){
            for(ProcessInsCcRead processInsCcRead:processInsCcReads){
                objIds+=processInsCcRead.getObjId()+",";
            }
            objIds=objIds.substring(0,objIds.lastIndexOf(","));
        }
        return objIds;
    }

    public void convert(List<ProcessInsCc>processInsCcs,String userIdCc){
        for(ProcessInsCc processInsCc:processInsCcs){
            boolean b=false;
            String hql="from ProcessInsCcRead where objId=? and userIdCc=?";
            List<ProcessInsCcRead>vos=processInsCcReadDao.find(hql,processInsCc.getId(),userIdCc);
            if(vos.size()>0){
                ProcessInsCcRead vo=vos.get(0);
                if(vo.getReadTime()!=null){
                    b=true;
                }
            }
            if(b){
                processInsCc.setReadStatus(ProccessInsCcStatus.READ.getStatus());
            }else {
                processInsCc.setReadStatus(ProccessInsCcStatus.NOREAD.getStatus());
            }
        }
    }
    /**
     * 更新阅读状态
     * @param objId
     * @param userId
     */
    @Override
    public void updateReadStatus(String objId,String userId) {
        String hql="from ProcessInsCcRead where objId=? and userIdCc=?";
        List<ProcessInsCcRead>vos=processInsCcReadDao.find(hql,objId,userId);
        if(vos.size()>0) {
            if(vos.size()>0){
                ProcessInsCcRead vo=vos.get(0);
                if(vo.getReadTime()==null){
                    hql = "update ProcessInsCcRead set readTime=? where objId=? and userIdCc=?";
                    processInsCcReadDao.execute(hql, new Date(), objId, userId);
                }
            }
        }
    }
}
